package com.beyondprototype.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    private TaskScheduler messageBrokerTaskScheduler;

    @Autowired
    public void setMessageBrokerTaskScheduler(@Lazy TaskScheduler taskScheduler) {
        this.messageBrokerTaskScheduler = taskScheduler;
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        //STOMP messages whose destination header begins with "/app" are routed to @MessageMapping methods in @Controller classes.
        // When messages are processed, the matching prefix is removed from the destination in order to form the lookup path.
        config.setApplicationDestinationPrefixes("/app");
        //Use the built-in memory-based message broker for subscriptions and broadcasting and route messages whose destination header begins with /topic or /queue to the broker.
        config.enableSimpleBroker("/topic", "/queue")
                // Configure the value for the heartbeat settings.
                // The first number represents how often the server will write or send a heartbeat.
                // The second is how often the client should write. 0 means no heartbeats.
                //By default this is set to "0, 0" unless the taskScheduler in which case the default becomes "10000,10000" (in milliseconds).
                .setHeartbeatValue(new long[] {10000,20000})
                //Configure the TaskScheduler to use for providing heartbeat support. Setting this property also sets the heartbeatValue to "10000, 10000".
                .setTaskScheduler(messageBrokerTaskScheduler);

        //Whether the client must receive messages in the order of publication.
        config.setPreservePublishOrder(true);

        //Configure the prefix used to identify user destinations.
        // User destinations provide the ability for a user to subscribe to queue names unique to their session as well as for others to send messages to those unique, user-specific queues.
        config.setUserDestinationPrefix("/user");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //the HTTP URL for the endpoint to which a WebSocket (or SockJS) client needs to connect for the WebSocket handshake.
        //e.g. brokerURL: 'ws://localhost:8090/websocket-demo-endpoint',
        registry.addEndpoint("/websocket-demo-endpoint");
    }
}
